#ifndef __MYUSART_H_
#define __MYUSART_H_

#include "main.h"
#include "stdio.h"
#include "usart.h"
#include "stdarg.h"
#include <string>

#define RxBuffLength 20
#define TxBuffLength 10

// 数据转换
typedef union
{
    float fdata;
    uint8_t udata[4];
} DataTypeConvert;

class MYUSART
{
private:
    // 普通串口参数
    UART_HandleTypeDef *__huart;
    uint8_t RxBuff[RxBuffLength];
    uint8_t RxState, length, RxData;
    bool __cpltflag;

public:
    uint8_t rxdata[RxBuffLength];
    DataTypeConvert DTC;
    uint8_t TxBuff[TxBuffLength];
    MYUSART(UART_HandleTypeDef *huart);
    // MYUSART(/* args */){}
    ~MYUSART() {}
    uint8_t ReceivePack(uint8_t beginwith, uint8_t endwith, uint8_t datalength);
    void SendPack(uint8_t beginwith, uint8_t endwith, uint8_t value);
    void SendPack(uint8_t *txbuff, uint8_t packsize);
    void Set(void);
    void Reset(void);
    bool GetFlag(void);
    void myprintf(char *fmt, ...);
    void start_rx(void);
    void printinfo(void);
    void ClearUserData(void);
    uint8_t getCheckSum(uint8_t *buffer, uint8_t size);
    void ReceivePack_DMA(uint8_t beginwith, uint8_t endwith, uint8_t datalength);
};

extern MYUSART Serial;

#endif
